package game;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

//import com.enso.CoM.entities.Player;
import gfx.Camera;
import gfx.GUI;
import gfx.Spritesheet;
import util.GameVars;
import util.InputHandler;
import util.Logger;

public class Game {
	private static FullscreenActivity activity;
	SplashScreen splash;
	private Menu menu;

//	private GUI gui;
	private Stage currentStage;
	
//	private Player player;

	private short gameLayer;
//	private boolean inStage;
	private boolean isReady = false;
	private boolean splashReady = false;
	private boolean close = false;
	
	public Game(){
		splash = new SplashScreen();
		
		menu = new Menu();
//		gui = new GUI();
//		inStage = false;
		
//		currentStage = null;
		
//		gameLayer = GameVars.GAME_LAYER_MENU;
		gameLayer = GameVars.GAME_LAYER_SPLASH;
	}

	public void update(long time){
		InputHandler.getInstance().tick();
		
		if(close)
			activity.quickKill();

		if(Spritesheet.getInstance().isLoaded()){
			isReady = true;
		}
//		if(gui == null)
//			gui = new GUI();
		
		if(isReady && splashReady)
			if(gameLayer <= GameVars.GAME_LAYER_MENU)
				if(!menu.isLoaded())
					menu.load();
		
//		if(gui != null)
//			if(gui.isChanged())
//				gui.update(time);
		
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			if(splash.next()){
				gameLayer = GameVars.GAME_LAYER_MENU;
				splash = null;
			}
			break;
		case GameVars.GAME_LAYER_STAGE:
//			if(gui == null){
//				gui = new GUI();
//			}
			
			if(currentStage == null){
				currentStage = new Stage(menu.getStageSelection(), menu.getDifficultySelection());
				menu = null;
			}
			if(currentStage.back()){
				menu = new Menu();
				menu.loadMenu((short) 0, (short) 0, (short) 0);
				currentStage = null;
				gameLayer = GameVars.GAME_LAYER_MENU;
				break;
			}
			if(currentStage.kill()){
				currentStage = null;
				menu = new Menu();
				gameLayer = GameVars.GAME_LAYER_MENU;
				break;
			}
			currentStage.update(time);
			break;
		case GameVars.GAME_LAYER_MENU:
			if(menu.inMenu()){
				menu.update(time);
				close = menu.close();
				break;
			}
			else {
				gameLayer = menu.getNextGameLayer();
				Logger.log("Layer: " + gameLayer);
				break;
			}
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		}
		
		
//		if(!inStage){
//			if(currentStage != null)
//				if(currentStage.isLoaded())
//					inStage = true;
//		}
//		else{
//			if(currentStage == null)
//				inStage = false;
//		}
//		
//		if(inStage)
//			currentStage.update(time);
	}
	
	public void draw(GL10 gl){
		if(gameLayer == GameVars.GAME_LAYER_SPLASH)
			if(!Spritesheet.getInstance().isSplashLoaded()) return;
			else {
				gl.glPushMatrix();

				gl.glTranslatef(-Camera.GL_WIDTH/2, -Camera.GL_HEIGHT/2,  Camera.Z_DISTANCE);
				
				splash.draw(gl);
				
				gl.glPopMatrix();
				
				splashReady = true;
			}
		if(!Spritesheet.getInstance().isLoaded()) return;
		gl.glPushMatrix();

		gl.glTranslatef(-Camera.GL_WIDTH/2, -Camera.GL_HEIGHT/2,  Camera.Z_DISTANCE);
		
		switch(gameLayer){
//		case GameVars.GAME_LAYER_SPLASH:
//			splash.draw(gl);
//			break;
		case GameVars.GAME_LAYER_STAGE:
			if(currentStage == null || !currentStage.isLoaded()) break;
			currentStage.draw(gl);
//			gui.draw(gl);
			break;
		case GameVars.GAME_LAYER_MENU:
			if(menu.isLoaded())
				menu.draw(gl);
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		}
		
//		gl.glTranslatef(-Camera.GL_WIDTH/2, -Camera.GL_HEIGHT/2,  Camera.Z_DISTANCE);
		gl.glPopMatrix();

//		if(inStage)
//			currentStage.draw(gl);
	}
	
	public boolean loadGraphics(GL10 gl){
		
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
			if(currentStage == null || !currentStage.isLoaded()) break;
			if(!currentStage.hasGraphicsLoaded()){
				currentStage.loadGraphics(gl);
				return false;
			}
			break;
		case GameVars.GAME_LAYER_MENU:
//			if(menu.inMenu()){
//				menu.update(time);
//				close = menu.close();
//				break;
//			}
//			else {
//				gameLayer = menu.getNextGameLayer();
//				Logger.log("Layer: " + gameLayer);
//				break;
//			}
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		}
		return true;
	}
	
	public Stage getStage(){ return currentStage; }
	public boolean isReady(){ return isReady; }
	public void setContext(FullscreenActivity c){ activity = c; }
	public boolean close(){ return close; }
	
	public void pause(){
		
	}
	
	public boolean handleBackPress(){
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			return true;
		case GameVars.GAME_LAYER_STAGE:
			return currentStage.handleBackPress();
		case GameVars.GAME_LAYER_MENU:
			return menu.handleBackPress();
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		}
		return false;
	}

	public void handleUp(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			if(menu.isLoaded()){
				splash.handleUp(location);
			}
			break;
		case GameVars.GAME_LAYER_STAGE:
			currentStage.handleUp(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleUp(location);
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		default:
			break;
		}		
	}

	public void handleDrag(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
			currentStage.handleDrag(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleDrag(location);
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		default:
			break;
		}		
	}

	public void handleTouch(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
			currentStage.handleTouch(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleTouch(location);
			break;
		case GameVars.GAME_LAYER_HIGH_SCORES:
			break;
		case GameVars.GAME_LAYER_OPTIONS:
			break;
		case GameVars.GAME_LAYER_EXTRAS:
			break;
		default:
			break;
		}		
	}
}
